package com.sys.websocket;

import com.sys.guacamole.ColorScheme;
import com.sys.guacamole.Guacamole;
import com.sys.guacamole.GuacamoleRDP;
import com.sys.guacamole.GuacamoleSSH;
import com.sys.utils.MongodbUtils;
import com.sys.utils.Utils;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.apache.guacamole.websocket.GuacamoleWebSocketTunnelEndpoint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@ServerEndpoint(value = "/sys/webSocketTunnel", subprotocols = "guacamole", configurator = WebSocketConfig.class)
@Component
public class WebSocketTunnel extends GuacamoleWebSocketTunnelEndpoint {
    private static String serviceHostname;
    private static Integer servicePort;

    @Value("${guacamole.serviceHostname}")
    public void setServiceHostname(String serviceHostname) {
        this.serviceHostname = serviceHostname;
    }
    @Value("${guacamole.servicePort}")
    public void setServicePort(Integer servicePort) {
        this.servicePort = servicePort;
    }

    @Override
    protected GuacamoleTunnel createTunnel(Session session, EndpointConfig endpointConfig) throws GuacamoleException {
        String protocol = (String) session.getUserProperties().get("protocol");
        String hostname = (String) session.getUserProperties().get("hostname");
        Integer port = (Integer) session.getUserProperties().get("port");
        Integer width = (Integer) session.getUserProperties().get("width");
        Integer height = (Integer) session.getUserProperties().get("height");

        if (serviceHostname == null){
            serviceHostname = "192.168.3.40";
        }
        if (servicePort == null){
            servicePort = 4822;
        }

        GuacamoleTunnel guacamoleTunnel;
        if (protocol != null && protocol.toLowerCase().equals("rdp")) {
            if (port == null || port <= 0) {
                port = 3389;
            }
            GuacamoleRDP guacamoleRDP = new GuacamoleRDP(serviceHostname, servicePort);
            guacamoleRDP.setGuacamoleConfiguration(hostname, port);
            guacamoleRDP.displaySettings(null, width, height, null, null);
            guacamoleTunnel = guacamoleRDP.getGuacamoleTunnel();
        } else if (protocol != null && protocol.toLowerCase().equals("ssh")) {
            if (port == null || port <= 0) {
                port = 22;
            }
            GuacamoleSSH guacamoleSSH = new GuacamoleSSH(serviceHostname, servicePort);
            guacamoleSSH.setGuacamoleConfiguration(hostname, port);
            guacamoleSSH.displaySettings(ColorScheme.WhiteBlack, "Consolas", 12);
            guacamoleTunnel = guacamoleSSH.getGuacamoleTunnel();
        } else {
            Guacamole guacamole = new Guacamole(serviceHostname, servicePort);
            guacamole.setGuacamoleConfiguration(protocol, hostname, port);
            guacamoleTunnel = guacamole.getGuacamoleTunnel();
        }

        return guacamoleTunnel;
    }

}